﻿namespace AjLogo.Primitives
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using AjLogo.Language;

    public class First : UnaryPrimitive
    {
        public override object Apply(BindingEnvironment environment, object value)
        {
            if (value == null)
                return ErrorValue.Instance;

            ListValue list = value as ListValue;

            if (list != null)
                return list.First;

            string text = value as string;

            if (text != null)
                if (string.IsNullOrEmpty(text))
                    return value;
                else
                    return text.Substring(0, 1);

            return ErrorValue.Instance;
        }
    }
}
